/* ************************************************************************************************

	Copyright (c) 2005-2006, IGR
	All Rights Reserved.
	Licensed under the Apache License version 2.0 or above.
	For more information on licensing, see:
        http://www.apache.org/licenses/


 ************************************************************************************************
 Objet SIMPLE WINDOW
 VERSION:0.85
 DATE:16/03/2006
 ************************************************************************************************
Init parameter in objPar (object of parameter):
 Name:               window name
 top:           	 Top position (in pixel)
 left:               Left position (in pixel)             
 width:              Width 
 height              height
 title				 title of the window
 icone				 image in the title bar 
 style				 style of window
 parent              object parent  		        
*************************************************************************************************/


// *************************************
// CONSTRUCTOR
// *************************************6

rialto.widget.SimpleWindow=function(objPar){
//=============================
	
    //base object
    this.base = rialto.widget.AbstractContainer;    
    objPar.type = "SimpleWindow";
    this.base(objPar) ;
 
	//Default value 	
	this.icone	= rialtoConfig.pathRialtoE +"images/imgFenSimple/picto-gr_default.gif"
    this.title="Window";
    this.style="fenetre";
    this.autoResizeParent= true; 
    this.autoResizableH=true;
    this.autoResizableW=true;
    
	//set the value with the properties of the objPar object  
	if (rialto.lang.isString(objPar.title)) {
       this.title=objPar.title;
    }     				
    if (rialto.lang.isString(objPar.icone)) {
       this.icone= rialtoConfig.pathRialtoE +objPar.icone;
    }
    if (rialto.lang.isString(objPar.icone)) {
       this.icone= rialtoConfig.pathRialtoE +objPar.icone;
    }
    if (rialto.lang.isStringIn(objPar.style,["default","fenetre"])){
    this.style= objPar.style;
    }    
    //if there is a visible window we hide it (problem of performance with too much elements ???)
    if (rialto.widget.SimpleWindow.prototype.openWindow){
    	rialto.widget.SimpleWindow.prototype.openWindow.setVisible(false);;         
	} 
    
    // *******************************************
	// GENERATION OF DOM ELEMENTS
	// *******************************************
	 

    this.divExt.style.top= this.top || "0"
    this.divExt.style.left= this.left || "0";        
    this.divExt.style.position=this.position;        
    this.divExt.style.width = this.width || "100%";        
    this.divExt.style.height= this.height || "100%"; 
    //this.divExt.style.border="1px solid red";
	this.divExt.style.overflow='hidden';                       
    if (objPar.parent &&  !this.bWithoutPlaceIn) {
		this.placeIn(objPar.parent);
	};    
	
	//release object
	objPar=null;    
}
rialto.widget.SimpleWindow.prototype = new rialto.widget.AbstractContainer;

rialto.widget.SimpleWindow.prototype.arrOpenWidow=new Array();
rialto.widget.SimpleWindow.prototype.openWindow=null;

// ***********************************************************
// UPDATE ELEMENTS AFTER DOM INSERTION
// ***********************************************************

rialto.widget.SimpleWindow.prototype.adaptToContext = function(parent) {
        
        //initial height        
        this.divExt.style.height = this.getNewParentHeight();                        
        
        var oThis=this;
        //ADD the Div3D style
        oParent =this.divExt;
        this.deco=new objDiv3D(this.title,this.style);
        this.deco.createDiv3D(oParent);

        //icon
        this.DivIcone=this.deco.DivIconeG;
        this.DivIcone.className="FenDivIcone";        
        this.icoFen = new rialto.widget.Image(this.icone,0,0,this.DivIcone);
        //get the title area
        this.divTitle=this.deco.DivTitre;
        this.divTitle.className="FenDivTitre";         
        //get content area
        //this.contenuFenetre=this.deco.DivContenu;        
        this.contenuFenetre=document.createElement("DIV");
        this.divExt.appendChild(this.contenuFenetre);
        this.contenuFenetre.style.top=50;
        this.contenuFenetre.style.left=15;
        this.contenuFenetre.style.height=this.getNewParentHeight()-65+'px';        
        this.contenuFenetre.style.width=this.getNewParentWidth()-30+'px';        
        this.contenuFenetre.style.position="absolute";
        this.contenuFenetre.style.overflow='auto';
		//this.contenuFenetre.style.border="1px solid red";	    
        //close buton
        this.btnClose = new rialto.widget.Image("btonFenFermOff",-20,0,this.deco.DivIconeD,"Fermer la fenetre","btonFenFermOn");
        this.btnClose.onclick=function(){oThis.closeWindow();}                
        //add to the prototype array of open window
        rialto.widget.SimpleWindow.prototype.arrOpenWidow.push(this);
        //enable the window
        this.activeFen();
}


rialto.widget.SimpleWindow.prototype.release=function(){
    this.btnClose.onclick=null;
    if (rialto.widget.SimpleWindow.prototype.openWindow==this){
	    rialto.widget.SimpleWindow.prototype.openWindow=null;
    }
}

rialto.widget.SimpleWindow.prototype.getHtmlCont=function(){
    //function return the html element container of the window
    return this.contenuFenetre;
}


rialto.widget.SimpleWindow.prototype.updateSize= function(){       
	
    this.contenuFenetre.style.overflow='hidden';
    if (this.autoResizableH || this.autoResizeContenu){            
        this.updateHeight();        
    }
     //si auto Width
    if(this.autoResizableW){        
        this.updateWidth();
    }    
    this.contenuFenetre.style.overflow='auto';
    
}

rialto.widget.SimpleWindow.prototype.updateWidth=function(){  	
	this.contenuFenetre.style.overflow='hidden';
    //get new width of the parent
    var tailleCalc=this.getNewParentWidth();
    this.divExt.style.width= tailleCalc;
    this.contenuFenetre.style.width= tailleCalc-30;              
    //call childs resize
    this.resizeChilds(true,false);         
    this.contenuFenetre.style.overflow='auto';
}

rialto.widget.SimpleWindow.prototype.updateHeight=function(){        
	this.contenuFenetre.style.overflow='hidden';
    //get new height of the parent
    var tailleCalc=parseInt(this.getNewParentHeight());
    this.divExt.style.height= tailleCalc;
    this.contenuFenetre.style.height= tailleCalc-65;              
    //call childs resize
    this.resizeChilds(false,true);  
    this.contenuFenetre.style.overflow='auto';
}


//******METHOD ARE DEPRECATED...USE closeWindow
rialto.widget.SimpleWindow.prototype.closeFen=function(){   
    rialto.deprecated('SimpleWindow','closeFen','closeWindow');
    this.closeWindow();   
}

rialto.widget.SimpleWindow.prototype.closeWindow=function(){   
    //remove the window from the array
    rialto.array.remove(rialto.widget.SimpleWindow.prototype.arrOpenWidow,this);
    rialto.widget.SimpleWindow.prototype.openWindow=null;    
    //call onClose method
    this.onClose();
    //remove the window from DOM
    this.remove();            
    //enable last window of the array
    if (rialto.widget.SimpleWindow.prototype.arrOpenWidow.length>0){
        rialto.widget.SimpleWindow.prototype.arrOpenWidow[rialto.widget.SimpleWindow.prototype.arrOpenWidow.length-1].activeFen();
    }    
}       


rialto.widget.SimpleWindow.prototype.activeFen=function(){        
    if (rialto.widget.SimpleWindow.prototype.openWindow!=this){
        // on masque la fenetre active
        if (rialto.widget.SimpleWindow.prototype.openWindow){
          rialto.widget.SimpleWindow.prototype.openWindow.setVisible(false);
        }       
        this.divExt.style.display='block';
        this.updateSize();
        rialto.widget.SimpleWindow.prototype.openWindow=this;                  
        this.$$activeContent();
        this.onfocus();
    }    
}

 
//******those 2 METHODS ARE DEPRECATED...USE SETVISIBLE
//to enable
rialto.widget.SimpleWindow.prototype.masque=function(){   
    rialto.deprecated('SimpleWindow','masque','setVisible(false)');
    this.setVisible(false);   
}
//to desable
rialto.widget.SimpleWindow.prototype.demasque=function(){
   rialto.deprecated('SimpleWindow','demasque','setVisible(true)');
   this.setVisible(true);   
}
//******

//this method will allow to display/hide the window according to the parameter
rialto.widget.SimpleWindow.prototype.setVisible=function(visible){
//=======================================================        
    var oHtml=this.getHtmlExt();
    if(visible){ 
    	rialto.widget.SimpleWindow.prototype.openWindow=this;    
        oHtml.style.display='block';
        this.updateSize();
    }
    else{
	    rialto.widget.SimpleWindow.prototype.openWindow=null; 
        oHtml.style.display='none';
        this.onblur();
    }
    this.visible=visible;
}


//change the title
rialto.widget.SimpleWindow.prototype.setTitle=function(newtitle) {    
	this.divTitle.replaceChild(document.createTextNode(newtitle),this.divTitle.firstChild);
    this.onSetTitle(newtitle);
}

rialto.widget.SimpleWindow.prototype.onSetTitle=function(newtitle){
//The code must be redefine after the instanciation of the component. By default this method does nothing. 
}

rialto.widget.SimpleWindow.prototype.onClose=function(){
//The code must be redefine after the instanciation of the component. By default this method does nothing. 
}